package com.xinggq.auth.api.exception;

import com.xinggq.common.exception.AbstractCommonException;
import com.xinggq.common.enums.ResponseEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;

/**
 * 认证模块专用异常类
 * 继承CommonException，用于认证模块的业务异常处理
 * 遵循异常处理最佳实践：记录日志、包装异常、避免异常被吞掉
 *
 * @author xinggq
 * @since 2025-10-21
 */
@Getter
@Setter
public class AuthException extends AbstractCommonException {

  @Serial
  private static final long serialVersionUID = 1L;

  private AuthException(ResponseEnum responseEnum) {
    super(responseEnum);
  }

  private AuthException(ResponseEnum responseEnum, String message) {
    super(responseEnum, message);
  }

  private AuthException(ResponseEnum responseEnum, Throwable cause) {
    super(responseEnum, cause);
  }

  private AuthException(ResponseEnum responseEnum, String message, Throwable cause) {
    super(responseEnum, message, cause);
  }

  // ==================== 静态工厂方法 ====================

  /**
   * 用户不存在异常
   */
  public static AuthException userNotFound(String username) {
    return new AuthException(ResponseEnum.AUTH_USER_NOT_FOUND, "用户不存在: " + username);
  }

  /**
   * 用户名或密码错误异常
   */
  public static AuthException userOrPasswordError() {
    return new AuthException(ResponseEnum.AUTH_USER_ERROR);
  }

  /**
   * 用户被禁用异常
   */
  public static AuthException userDisabled(String username) {
    return new AuthException(ResponseEnum.AUTH_USER_DISABLE, "用户已被禁用: " + username);
  }

  /**
   * 认证失败异常
   */
  public static AuthException authFailure(String message) {
    return new AuthException(ResponseEnum.AUTH_FAILURE, message);
  }

  /**
   * 权限不足异常
   */
  public static AuthException noPermission(String operation) {
    return new AuthException(ResponseEnum.AUTH_NO_PERMISSION,
        "权限不足，无法执行操作: " + operation);
  }

  /**
   * 验证码错误异常
   */
  public static AuthException verifyCodeError() {
    return new AuthException(ResponseEnum.AUTH_VERIFYCODE_ERROR);
  }

  /**
   * 验证码过期异常
   */
  public static AuthException verifyCodeExpired() {
    return new AuthException(ResponseEnum.AUTH_VERIFYCODE_EXPIRE);
  }

  /**
   * 异常登录异常
   */
  public static AuthException abnormalLogin() {
    return new AuthException(ResponseEnum.ABNORMAL_LOGIN_ERROR);
  }

  /**
   * 账户锁定异常
   */
  public static AuthException accountLocked() {
    return new AuthException(ResponseEnum.ABNORMAL_LOGIN_ERROR_LOCK);
  }
  
  /**
   * 服务调用失败异常
   * 用于处理微服务间调用失败的情况，如用户中心服务不可用
   */
  public static AuthException serviceCallFailed(String message) {
    return new AuthException(ResponseEnum.SERVICE_UNAVAILABLE, message);
  }
  
  /**
   * 服务调用失败异常（带原始异常）
   * 用于处理微服务间调用失败的情况，保留原始异常信息
   */
  public static AuthException serviceCallFailed(String message, Throwable cause) {
    return new AuthException(ResponseEnum.SERVICE_UNAVAILABLE, message, cause);
  }
  
  /**
   * 无效凭证异常
   * 用于处理用户名或密码错误的情况
   */
  public static AuthException invalidCredentials(String message) {
    return new AuthException(ResponseEnum.AUTH_USER_ERROR, message);
  }
  
  /**
   * 无效令牌异常
   * 用于处理令牌验证失败的情况
   */
  public static AuthException invalidToken(String message) {
    return new AuthException(ResponseEnum.AUTH_FAILURE, message);
  }
  
  /**
   * 操作失败异常
   * 用于处理一般性操作失败的情况
   */
  public static AuthException operationFailed(String message) {
    return new AuthException(ResponseEnum.REQUEST_FAIL, message);
  }
  
  /**
   * 会话冲突异常
   * 用于处理重复登录或会话冲突的情况
   */
  public static AuthException sessionConflict(String message) {
    return new AuthException(ResponseEnum.AUTH_FAILURE, message);
  }
}